package cpcn.dsp.demo.tx.liveness.notify;


import cpcn.dsp.demo.consts.DSPConsts;
import cpcn.dsp.institution.api.notice.NoticeRequest;
import cpcn.dsp.institution.api.notice.NoticeResponse;
import cpcn.dsp.institution.api.system.DSPInstitutionEnvironment;
import sun.misc.BASE64Encoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @ClassName: ReceiveNoticeBackground
 * @Description: 接收后台通知
 * @Author: chengzi
 * @Date: 2020/7/2 0002 13:02
 * @Version: v1.0 文件初始创建
 */

public class ReceiveNoticeBackground extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) {
        String message = request.getParameter("Message");
        String signature = request.getParameter("Signature");
        String dgtlenvlp = request.getParameter("Dgtlenvlp");
        String signSN = request.getParameter("SignSN");
        String encryptSN = request.getParameter("EncryptSN");
        //解析
        //1.初始化
        try {
            DSPInstitutionEnvironment.initialize(DSPConsts.Keystore_test, DSPConsts.myKeystorePassword_test, DSPConsts.publicKey_test, DSPConsts.MSG_ENCRYPT_20);
            NoticeRequest noticeRequest = new NoticeRequest(message, signature, dgtlenvlp, signSN, encryptSN);
            String plainText = noticeRequest.getPlainText();
            String txCode = noticeRequest.getTxCode();
            String code = noticeRequest.getCode();
            String noticeMessage = noticeRequest.getMessage();
            System.out.println(plainText);

            ////////////////////////////////////////
            //////后台通知需要响应给数据服务SUCCESS/////
            ////////////////////////////////////////
            PrintWriter out = response.getWriter();
            BASE64Encoder encoder = new BASE64Encoder();
            String noticeResponse = NoticeResponse.getMessage();
            String data = encoder.encode(noticeResponse.getBytes("UTF-8"));
            out.write(data);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
